<?php
class AdCategoriesController extends AppController {
	public $name        = 'AdCategories';
    public $helpers     = array('Html', 'Cache', 'Form', 'Javascript');
    public $cacheAction = array('index/' => '60'); // cache index View for 1 minute

    public function admin_index() {
        //$this->paginate = array('limit'=>2);
        $this->set('adCategories', $this->paginate());
    } // admin_index()

    public function admin_add () {
        if (!empty($this->data)) {
            $this->AdCategory->set($this->data);

            if (!$this->AdCategory->validates()) {
                $json = $this->AdCategory->invalidFields();
            } else if ($this->AdCategory->save($this->data)) {
                $this->flash(__('Album saved.', true), array('action'=>'index'));
            }
        }
    } // admin_add

    public function admin_axAdd () {
        Configure::write('debug', 0);
        $this->AdCategory->set($this->data);
        $model = 'AdCategory';
        $stt   = -1;

        if ($this->AdCategory->validates()) {
            if ($this->AdCategory->save($this->data)) {
                $stt             = 1;
                $json['message'] = __('New category saved.', true);
            }
        } else {
            $err = $this->AdCategory->invalidFields();
            foreach ($err as $k=>$v) {
               $json['error'][] = array('id'=>$model.ucwords($k), 'field'=>$k, 'code'=>$v);
            }
        }

        header('X-JSON: { status:'.$stt.' }');
        $this->set('json', $json);
        $this->render(false, 'json');
    } // admin_axAdd

    public function admin_edit ($id=null) {
        if (empty($this->data)) {
            if (!$id) {
                //$this->Session->setFlash('<h4>'.__('Missing required id',true).'</h4>', 'default', array('class'=>'boxError'));
                $this->flash('<h4>'.__('Missing required id',true).'</h4>', 'index', 5);
            } else {
                $this->data = $this->AdCategory->findById($id);
            }
        } else {
            if ($this->AdCategory->save($this->data)) {
                $this->Session->setFlash('<h4>'.__('The Category has been saved.',true).'</h4>', 'default', array('class'=>'boxSuccess'));
                $this->redirect('/admin/adCategories');
            }
        }
    } // admin_edit

    public function admin_delete ($id) {
        if (!$id) {
            $this->set('pageTitle', __('Missing Post Id.',true));
            $this->flash('<h4>'.__('Missing Post Id.',true).'</h4>', '/admin/adCategories', 5);
            return false;
        } else if ($this->AdCategory->del($id)) {
            $this->Session->setFlash('<h4>'.__('Category has been deleted',true).'</h4>', 'default', array('class'=>'boxSuccess'));
            $this->redirect('/admin/adCategories');
        }
    } // admin_delete

    public function admin_view ($id=null) {
        if (!$id) {
            $this->Session->setFlash('<h4>'.__('Missing required id',true).'</h4>', 'default', array('class'=>'boxError'));
        }
        $this->set('adCategory', $this->AdCategory->findById($id));
    } // admin_view


    public function index () {
        $this->pageTitle = __('Free Classifieds in Your City', true);

        App::import('Model', 'Country');
        $country = new Country();
        $this->set('countries', $country->find('list'));

        App::import('Model', 'Location');
        $location = new Location();

        $this->set('areas', $location->find(
            'list',
            array('conditions' => array('parent_id'=>null))
        ));

        $rs = $this->AdCategory->find('list', array(
            'fields' => array('AdCategory.id', 'AdCategory.category', 'AdCategory.parent'),
            'order'  => 'AdCategory.category ASC'
        ));

        $sql = 'SELECT COUNT(*) AS `count`, `ad_category_id` FROM `ad_posts` GROUP BY `ad_category_id` ORDER BY `ad_category_id`';
        $res = $this->AdCategory->query($sql);
        $cnt = array();
        foreach ($res as $tmp) {
            $id = $tmp['ad_posts']['ad_category_id'];
            $cnt[$id] = $tmp[0]['count'];
        }

        $this->set('categories', $rs);
        $this->set('postcount', $cnt);
    } // index

    public function selectCategory () {
        $this->pageTitle = __('Select a Category', true);

        $rs = $this->AdCategory->find('list', array('fields'=>array('AdCategory.id', 'AdCategory.category', 'AdCategory.parent')));
        $this->set('categories', $rs);
    } // selectCategory

    public function mainCategories () {
        $categories = $this->AdCategory->mainCategories();

        if (isset($this->params['requested'])) { // call from View Element
            foreach ($categories as $c) {
                $options[$c] = $c;
            }
            return $options;
        }

        $this->set('categories', $categories);
    } // mainCategories

} // AdCategoriesController
?>